<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>VUE第一天 v-for</title>
  <style>
    .table {
      border-collapse: collapse;
    }
  </style>
</head>
<body>
  <div id="app"> 
    <h2>TODO List</h2>
    <input type="text" v-model="thing" @keyup.enter="add">
    <button @click="add">添加</button>
    <div v-show="todoThings.length">
      <p>待完成事项</p>
      <ul>
        <li v-for="(item, index) in todoThings">
          <button @click="done(index)">完成</button>
          <span>{{item}}</span>
        </li>
      </ul>
    </div>
    <div v-show="doneThings.length">
      <p>已完成事项</p>
      <ul>
        <li v-for="(item, index) in doneThings">
          <span>{{item}}</span>
        </li>
      </ul>
    </div>
    <hr>
    <button @click="addTableData">新增</button>
    <table class="table" border="1">
      <tr>
        <td>序号</td>
        <td>姓名</td>
        <td>电话</td>
        <td>操作</td>
      </tr>
      <tr v-for="(item, index) in tableData">
        <td v-for="val in item">{{val}}</td>
        <td><button @click="del(index)">删除</button></td>
      </tr>
    </table>
    <div v-show="showNewInput">
      <input type="text" v-model="num">
      <input type="text" v-model="name">
      <input type="text" v-model="tel">
      <button @click="makeSure">确认</button>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        // 输入框中的值
        thing: '',
        // 待完成
        todoThings:[],
        // 已完成
        doneThings:[],
        // 表单数据
        tableData: [],
        // 是否显示新增的框
        showNewInput: false,
        num: '',
        name: '',
        tel: ''
      },
      created() {
        this.tableData = localStorage.getItem('tableData')
      },
      methods: {
        // 添加事件
        add() {
          if(this.thing) {
            this.todoThings.push(this.thing);
            // 清空输入框
            this.thing = '';
          }
        },
        // 完成事件
        done(index) {
          // 加到已完成中
          this.doneThings.push(this.todoThings[index]);
          // 从待办中删除
          this.todoThings.splice(index, 1);
        },
        // 给表单增加数据
        addTableData() {
          this.showNewInput = true;
        },
        // 确认添加
        makeSure() {
          this.tableData.push({
            num: this.num,
            name: this.name,
            tel: this.tel
          });
          this.num = '';
          this.name = '';
          this.tel = '';
          // 将数据保存到localStorage中
          localStorage.setItem('tableData', JSON.stringify(this.tableData))
        },
        // 删除用户
        del(index) {
          this.tableData.splice(index, 1);
        }
      },
    });
  </script>
</body>
</html>